home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
A.C.E. 2
/
ACE CD 2.iso
/
FILES
/
UTILS
/
PROCAL13.DMS
/
PROCAL13.adf
/
Rexx
/
replace.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1991-12-11
|
10KB
|
245 lines
/* Replace S. Dicker 8-Aug-89 */
/* */
/* ARexx program used to replace all occurrences of one string */
/* with another in a selected range of cells. This program can be */
/* used, for example, to modify all formulas that refer to cell C1 */
/* so that they refer to cell B1 instead. Select the range of */
/* cells to be modified and then issue the command: */
/* */
/* rx replace <oldtext> <newtext> */
/* */
/* where <oldtext> is the original string */
/* <newtext> is the replacement string */
/* */
/* NOTE: String comparison/replacement is done in upper case only. */
/* */
signal on error /* Trap host command errors. */
/* Explicitly say "parse arg", to avoid converting args to uppercase */
parse arg old_text new_text /* Retrieve command line arguments. */
address 'Advantage' /* Send commands to Advantage. */
options results /* Enable return of string results. */
/* If the required arguments are not supplied, display an error */
/* message and then quit. */
if old_text = "" | new_text = "" then
do
'DrawMessage'
"Usage: rx replace <oldtext> <newtext>"
exit 10
end
new_text = strip(new_text,'L') /* Strip leading blanks. */
'Current' /* Determine current selected range. */
range = result
colon_posn = pos(":",range) /* Look for colon delimiter in range. */
if colon_posn = 0 then /* If no colon, this is not a range. */
do /* Set the start and end cells to */
start_cell = range /* the selected cell. */
end_cell = range
end
else /* Otherwise, it is a range. */
/* Extract the start/end cells from the specified range. */
do
start_cell = left(range,colon_posn - 1)
end_cell = substr(range,colon_posn + 1)
end
start_column = GetColumn(start_cell) /* Determine range of columns */
end_column = GetColumn(end_cell) /* to be filled. */
start_row = GetRow(start_cell) /* Determine range of rows */
end_row = GetRow(end_cell) /* to be filled. */
/* Scan the selected range of cells for the string to be replaced. */
/* It's much faster to scan in column-major order if possible, */
/* because NextColumn() is slower than just adding one to rownum. */
/* Search all columns in the selected range. */
column = start_column /* Reset column pointer. */
do until c2d(column) > c2d(end_column)
do rownum = start_row to end_row
/* Build complete cell name from row and column. */
next_cell = column || rownum
/* Retrieve the current contents of this cell (if it contains */
/* a formula or label - values always return NULL). */
contents = GetContents(next_cell)
/* Replace all occurrences of the search string. */
new_contents = Replace(contents,old_text,new_text)
/* If the string has changed, write it back to the cell. */
if new_contents ~= contents then
do
'SelectCell'
value(next_cell)
'PutCell'
new_contents
end
end
/* Advance to the next column in this row. */
column = NextColumn(column)
end
/* Reselect the original range of cells. */
'SelectRange'
value(start_cell)
value(end_cell)
exit /* That's all folks! */
/* >>> Host command error handler <<< */
Error:
exit rc /* Just bail out and return error code. */
/* ----------------------------------------------------------------- */
/* Internal Functions (subroutines) */
/* ----------------------------------------------------------------- */
/* == GetRow: Extract the row number from a cell name. == */
GetRow: procedure
arg CellName /* Function expects to be passed a cell name. */
/* Find the first numeric digit in the cell name. */
start_number = verify(CellName,"0123456789","Match")
/* Extract all characters starting at the first digit and continuing */
/* to the end of the cell name. */
rownum = substr(CellName,start_number)
return rownum
/* == GetColumn: Extract the column label from a cell name. == */
GetColumn: procedure
arg CellName /* Function expects to be passed a cell name. */
/* Find the first numeric digit in the cell name. */
start_number = verify(CellName,"0123456789","Match")
/* Extract all characters in the cell name up to the first numeric */
/* character (start of row number). */
column = left(CellName,start_number-1)
return column
/* == NextColumn: Given the current column label, determine the label == */
/* == for the next sequential column. NOTE: This is a == */
/* == recursive function. == */
NextColumn: procedure
arg ThisColumn /* Function expects to be passed a column label. */
/* If the column label is empty (null string), then we must be */
/* starting a new group of column labels (for example, going */
/* from column ZZ to column AAA. Return an "A". */
if length(ThisColumn) = 0 then
new_column = "A"
/* Otherwise, we must advance the last character in the column */
/* name to the next sequential alphabetic character. */
else
do
col_number = , /* Convert last character */
c2d( right(ThisColumn,1) ) /* to decimal value. */
col_number = col_number + 1 /* Increment to next character. */
new_char = d2c(col_number) /* Convert back to a character. */
/* If we've gone past 'Z', find the next column for the column */
/* label minus the last character and then append an "A" to */
/* this label (start of a new label set). */
if new_char > "Z" then
do
temp_column = left( ThisColumn, length(ThisColumn)-1 )
new_column = NextColumn(temp_column) || "A"
end
/* Otherwise, replace the last character of the column label */
/* with the next sequential character. */
else
new_column = overlay(new_char,ThisColumn,length(ThisColumn))
end
return new_column
/* == GetContents: Extract the contents of a specified cell. == */
/* == If the cell contains a "value" return a == */
/* == null (empty) string. == */
GetContents: procedure
arg CellName /* Function expects to be passed a cell name. */
'SelectCell' /* Select the cell whose contents we want. */
value(CellName)
'IsLabel' /* Does this cell contain a "label". */
lab_flag = result
'IsFormula' /* Does this cell contain a "formula". */
form_flag = result
/* Now extract the cell contents based on what type of data it */
/* contains. */
select
when form_flag = 1 then /* A Formula ... */
do
'GetFormula'
cell_contents = result
end
when lab_flag = 1 then /* ... a Label ... */
do
'GetLabel'
cell_contents = result
end
otherwise /* ... or a Value. */
cell_contents = ""
end
return cell_contents /* Return the cell contents. */
/* == Replace: Replace all occurrences of oldstring with newstring == */
/* == in the supplied string. == */
Replace: procedure
/* Explicitly say "parse arg", to avoid converting it all to uppercase */
parse arg start_string, oldstring, newstring
upper oldstring /* We want case-independent search */
start_pos = 1 /* Where to begin next search. */
/* Repeat the search/replace process until we reach the end of the string. */
do until start_pos > length(start_string) | start_pos = 0
upper_text = upper(start_string) /* Convert string to upper case */
/* for case independent search. */
/* Look for the string to be replaced. */
start_pos = pos(oldstring,upper_text,start_pos)
/* If we find it, replace it. */
if start_pos ~= 0 then
do
/* Remove the old text. */
start_string =,
delstr( start_string, start_pos, length(oldstring) )
/* Insert the new text. */
start_string =,
insert( newstring, start_string, start_pos-1 )
/* Update location for start of next search to past */
/* the end of the replacement text. */
start_pos = start_pos + length(newstring)
end
end
return start_string